﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LicenseManager;
using Utilities;

using Microsoft.Win32;



namespace OfficeInCloudWord2007
{
    public partial class Registration : Form
    {
        private Utilities.DbManager m_DBManager = null;
        
        public Registration()
        {
            InitializeComponent();
        }

        public Registration(Utilities.DbManager m_Db)
        {
            m_DBManager = m_Db; 
            InitializeComponent(); 
        }

        private void buttonProcess_Click(object sender, EventArgs e)
        {
            if (ValidateInput() == true)
            {
                RegisterUser();

                RegistryKey kHKCU = Registry.CurrentUser;
                RegistryKey kFirstRun = kHKCU.OpenSubKey("Software\\UpriseApps\\OfficeInCloudExcel2007", true);
                if (kFirstRun != null)
                {
                    kFirstRun.SetValue("FirstRun", 0);
                }
                

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private bool ValidateInput()
        {
            if (FirstNameBox.Text == "")
            {
                MessageBox.Show("Please fill in First Name");
                return false;
            }
            if (LastNameBox.Text == "")
            {
                MessageBox.Show("Please fill in Last Name");
                return false;
            }
            if (EmailTextBox.Text == "")
            {
                MessageBox.Show("Please fill in Email");
                return false;
            }
            if (PhoneTextBox.Text == "")
            {
                MessageBox.Show("Please fill in Phone");
                return false;
            }
            return true; 
        }

        private void RegisterUser()
        {
            ZohoLicenseManager lm = new ZohoLicenseManager("info@upriseapps.com", "mamamilaramu");
            ZohoTypes.ZohoContact contact = null;

            lm.Login();

            contact = lm.SearchForContact(EmailTextBox.Text);
            if (contact == null)
            {
                contact = new ZohoTypes.ZohoContact();
                contact.LastName = LastNameBox.Text;
                contact.FirtsName = FirstNameBox.Text;
                contact.Email = EmailTextBox.Text;
                contact.Phone = PhoneTextBox.Text;
                contact.LastLogin = DateTime.Now;
                contact.Company = CompanyTextBox.Text; 

                lm.InsertContact(contact);
                
            }
            else
            {
                contact.LastLogin = DateTime.Now;
                lm.UpdateContact(contact); 
            }

            lm.AddRegisterContact(contact, m_DBManager);

            lm.Logout();
        }


    }

 
}
